package com.bilicraft.plugin.kexue.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;

public class BBSConfig {
	
	public static HourCmd[] hourCmd = new HourCmd[24];
	
	public static Map<Integer, HourCmd> weekCmd= new HashMap<Integer, HourCmd>();
	
	public static int timeZone = 0;
	
	public static String udPlugin;
	
	public static BBSConfig instance;
	
	private BBSConfig(){
		for(int i = 0; i < 24; i++){
			hourCmd[i] = new HourCmd(i, 8);
		}
	}
	
	public static BBSConfig getInstance(){
		if(instance == null){
			instance = new BBSConfig();
		}
		return instance;
	}
	
	@SuppressWarnings("unchecked")
	public void setAll(FileConfiguration fc){
		weekCmd.clear();
		for(int i = 0; i < 24; i++){
			hourCmd[i] = new HourCmd(i, 8);
		}
		Bukkit.getServer().getLogger().info("BiliBlackScience loading configs:");
		/* Read update route */
		udPlugin = (String) fc.getConfigurationSection("update").get("plugin");
		Bukkit.getServer().getLogger().info("- plugins site:"+udPlugin);
		/* Read base info */
		timeZone = Integer.parseInt((String) fc.getConfigurationSection("general").get("timezone"));
		Bukkit.getServer().getLogger().info("- Time-Zone(UTC+):"+timeZone);
		/* Read Hourly Cmds */
		ArrayList<String> al = (ArrayList<String>) fc.get("hours");
		String[] s0;
		Bukkit.getServer().getLogger().info("BiliBlackScience adding Hour Cmds:");
		for(String s1:al){
			s0 = s1.split("::");
			if(s0.length < 2){
				continue;
			}
			hourCmd[Integer.parseInt(s0[0])].cmds.add(s0[1]);	
			Bukkit.getServer().getLogger().info("- ["+s0[0]+"] "+s0[1]);
		}
		/* Read Weekday-Hourly Cmds */
		al = (ArrayList<String>) fc.get("weekdays");
		Bukkit.getServer().getLogger().info("BiliBlackScience adding Weekday Cmds:");
		for(String s1:al){
			s0 = s1.split("::");
			if(s0.length < 3){
				continue;
			}
			HourCmd hc = weekCmd.get(s0[0]+"-"+s0[1]);
			if(hc == null){
				hc = new HourCmd(Integer.parseInt(s0[0]), Integer.parseInt(s0[1]));
				weekCmd.put(Integer.parseInt(s0[0])*100+Integer.parseInt(s0[1]), hc);
			}
			hc.cmds.add(s0[2]);
			Bukkit.getServer().getLogger().info("- ["+s0[0]+"]["+s0[1]+"] "+s0[2]);
		}
		Bukkit.getServer().getLogger().info("Weekday Cmds added.");
	}
	
}
